class BookController < ApplicationController

  def index
    @all_books = Book.getAllBooks
    @popular_books = Book.getAllBooks
  end

  def show
    @book = Book.find(params[:id])
    @condition = Condition.find(@book.condition_id)
    @user = User.find(@book.user_id)
    @tag_list = @book.tag_list
    @tags = @book.tag_counts
  end

  def new
    @conditions = Condition.find(:all)
  end

  def create
    @book = Book.new(params[:book])
    setup_tags @book
    if @book.save
      flash[:notice] = 'Your book is ready to be traded'
      redirect_to :action => "index"
    else
      render :action => "new"
    end
  end

  def edit
    @book = Book.find(params[:id])
    @tag_list = @book.tag_list
  end

  def update
     @book = Book.find(params[:id])
    setup_tags @book
    if @book.update_attributes(params[:book])
      flash[:notice] = 'Updated'
      redirect_to :action => "index"
    else
      render :action => "edit"
    end
  end

  def destroy
  end

  def search
  end
  
  def tag
   @books = Book.find_tagged_with(params[:id], :match_all => true)
  end
  
 private  
  def setup_tags(book)
    unless params[:tag_list].blank?
      @book.tag_list = params[:tag_list].split(",")
    end
  end
  

    
  
end
